package cn.ll.service.base;

import cn.ll.domain.constant.SqlOrderEnum;
import cn.ll.dao.base.IBaseDAO;
import cn.ll.domain.base.BaseDomain;

import java.util.List;
import java.util.Map;

/**
 * Created by lixiangjun on 2018/5/17.
 */
public abstract class AbstractBaseService<T extends BaseDomain, D extends IBaseDAO<T>> implements IBaseService<T, D> {


    @Override
    public final int add(T entity) {
        enhanceNewCreateBaseDomain(entity);
        return getDao().insert(entity);
    }

    @Override
    public final int edit(T entity) {
        return getDao().update(entity);
    }

    @Override
    public final int delete(Object id) {
        return getDao().deleteById(id);
    }

    @Override
    public final int deleteByIds(List list) {
        return getDao().deleteByIds(list);
    }

    @Override
    public final T view(Object id) {
        return (T) getDao().fetch(id);
    }

    @Override
    public final int insert(T entity) {
        enhanceNewCreateBaseDomain(entity);
        return getDao().insert(entity);
    }

    @Override
    public final int update(T entity) {
        return getDao().update(entity);
    }

    @Override
    public final int updateNull(T entity) {
        return getDao().updateNull(entity);

    }

    @Override
    public final int deleteById(Object id) {

        return getDao().deleteById(id);
    }

    @Override
    public final int deleteByProperty(String property, Object value) {
        return getDao().deleteByProperty(property, value);

    }

    @Override
    public final T fetch(Object id) {
        return (T) getDao().fetch(id);
    }

    @Override
    public final T findOne(String property, Object value) {
        return (T) getDao().findOne(property, value, null, null);
    }

    @Override
    public final List findList(String property, Object value) {
        return getDao().findList(property, value, null, null);
    }

    @Override
    public final List findList(String property, Object value, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return getDao().findList(property, value, orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public final List findAll() {
        return getDao().findAll(null, null);
    }

    @Override
    public final List findAll(String orderBy, SqlOrderEnum sqlOrderEnum) {
        return getDao().findAll(orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public final List like(Map<String, Object> condition) {
        return getDao().like(condition, null, null, null);
    }

    @Override
    public final List like(Map<String, Object> condition, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return getDao().like(condition, orderBy, sqlOrderEnum.getAction(), null);
    }

    @Override
    public final void updateOrSave(T entity, Object id) {
        if (id != null) {
            getDao().update(entity);
        } else {
            enhanceNewCreateBaseDomain(entity);
            getDao().insert(entity);
        }
    }

    @Override
    public final T selectOne(String mapperId, Object obj) {
        return (T) getDao().selectOne(mapperId, obj);
    }

    @Override
    public final List selectList(String mapperId, Object obj) {
        return getDao().selectList(mapperId, obj);
    }

    @Override
    public final int count(Map condition) {
        return getDao().count(condition);
    }

    @Override
    public final T queryOne(Map condition) {
        return (T) getDao().queryOne(condition, null, null, null);
    }

    @Override
    public final T queryOne(Map condition, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return (T) getDao().queryOne(condition, orderBy, sqlOrderEnum.getAction(), null);
    }

    @Override
    public final List queryList(Map condition, String orderBy, String sortBy) {
        return getDao().queryList(condition, orderBy, sortBy, null);
    }

    @Override
    public final List queryPage(Map condition, int offset, int rows) {
        return getDao().queryPage(condition, offset, rows, null, null, null);
    }

    public List<T> queryPage(Map<String, Object> condition, int offset, int rows, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return getDao().queryPage(condition, offset, rows, orderBy, sqlOrderEnum.getAction(), null);
    }

    @Override
    public final int deleteByCondition(Map condition) {
        return getDao().deleteByCondition(condition);
    }

    @Override
    public final int updateMap(Map entityMap) {
        enhanceCreateBaseDomain(entityMap);
        return getDao().updateMap(entityMap);
    }

    @Override
    //@CachePut(key = "#entityMap.id") 暂时不加缓存，使用的主键自增，并且没有返回整个对象  TODO 可以使用编程式解决
    public final int insertMap(Map entityMap) {
        enhanceNewCreateBaseDomain(entityMap);
        return getDao().insertMap(entityMap);
    }

    @Override
    public final List listByPage(Map condition, int offset, int rows) {
        return getDao().queryPage(condition, offset, rows, null, null, null);
    }

    @Override
    public final List listByPage(Map condition, int offset, int rows, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return getDao().queryPage(condition, offset, rows, orderBy, sqlOrderEnum.getAction(), null);
    }


    private final T enhanceNewCreateBaseDomain(T entity) {
        entity.setCreateDate(System.currentTimeMillis());
        return entity;
    }

    private final Map enhanceCreateBaseDomain(Map entityMap) {
        entityMap.put("lastModDate", System.currentTimeMillis());

        return entityMap;
    }

    private final Map enhanceNewCreateBaseDomain(Map entityMap) {
        entityMap.put("createDate", System.currentTimeMillis());

        return entityMap;
    }

    // ---------------- 后加接口，在这里默认做空实现，避免实现类报错，如果需要使用这些特性，需要重载 -----------------//
    @Override
    public List<T> queryPage(Map<String, Object> condition, int offset, int rows, String orderBy, SqlOrderEnum sqlOrderEnum, Map<String, Object> selectorpage) {
        return getDao().queryPage(condition, offset, rows, orderBy, sqlOrderEnum.getAction(), selectorpage);
    }

    @Override
    public List<T> like(Map<String, Object> condition, String orderBy, SqlOrderEnum sqlOrderEnum, Map<String, Object> selector) {
        return getDao().like(condition, orderBy, sqlOrderEnum.getAction(), selector);
    }

    @Override
    public List<T> queryList(Map<String, Object> condition, String orderBy, String sortBy, Map<String, Object> selector) {
        return getDao().queryList(condition, orderBy, sortBy, selector);
    }

    @Override
    public T queryOne(Map<String, Object> condition, String orderBy, SqlOrderEnum sqlOrderEnum, Map<String, Object> selector) {
        return (T) getDao().queryOne(condition, orderBy, sqlOrderEnum.getAction(), selector);
    }


    /**
     * 通用的更新操作
     *
     * @param updateMap    需要更新的值
     * @param conditionMap 需要被更新的条件
     */
    public int updateByCondition(Map<String, Object> updateMap, Map<String, Object> conditionMap) {
        return getDao().updateByCondition(updateMap, conditionMap);
    }
}
